package com.bw.controller;

import com.bw.entity.Report;
import com.bw.entity.User;
import com.bw.service.ReportService;
import com.bw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("report")
public class ReportController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private UserService userService;

    @RequestMapping("toAddReport")
    public String toAddReport(){
        return "report";
    }

    /**
     * 新增日报
     * @param report
     * @param request
     * @return
     */
    @RequestMapping("addReport")
    @ResponseBody
    public String addReport(Report report, HttpServletRequest request){
        if(report.getContent()==null || report.getContent().trim().equals("")||report==null){
            throw new NullPointerException("report为空");
        }
        reportService.addReport(report);
        User user = (User) request.getSession().getAttribute("user");
        Integer sid = user.getId();
        Integer rid = report.getId();
        Map map = new HashMap();
        map.put("sid",sid);
        map.put("rid",rid);
        reportService.addMid(map);
        return "ok";
    }
    /**
     * 跳轉到查看日报列表
     * @return
     */
    @RequestMapping("ToReportlist")
    public String ToReportlist(){
        return "reportList";
    }

    /**
     * 查看日报列表
     * @return
     */
    @RequestMapping("ReportList")
    @ResponseBody
    public List<Report> ReportList(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Report> rlist = reportService.findReportListById(user.getId());
        return  rlist;
    }

    @RequestMapping("studentList")
    public String studentList(){
        return "studentlist";
    }

    @RequestMapping("ReportListById")
    public String ReportListById(Model model,Integer id){
        List<Report> rlist = reportService.findReportListById(id);
        User user = userService.findUserById(id);
        model.addAttribute("rlist",rlist);
        model.addAttribute("user",user);
        return  "StudentReport";
    }
}
